import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/pages/indexPage/welcome_page.dart';
import 'package:flutter_demo/utils/log_utils.dart';
import 'package:flutter_demo/utils/navigator_util.dart';
import 'package:flutter_demo/utils/sp_utils.dart';
import 'package:flutter_demo/widget/permission_request.dart';
import 'package:flutter_demo/widget/protocol_model.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

import 'first_guild_page.dart';

class IndexPage extends StatefulWidget {
  IndexPage({Key key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> with ProtocolModel {
  List<String> _list = [
    '为您更好的体验应用，需要获取您的手机存相机限，以保存您的一些偏好设置',
    '您已经拒绝权限，所以无法使用一些设置',
    '您已经拒绝权限，请在设置中同意App的存储权限',
    '其他错误'
  ];
  @override
  void initState() {
    super.initState();

    Future.delayed(Duration.zero, () {
      initData();
    });
    // // ignore: missing_return
  }

  void initData() {
    //当App运行在release环境时
    bool isLog = !bool.fromEnvironment("dart.vm.product");

    LogUtils.init(islog: isLog);

    NavigatorUtil.pushPageByFade(
        context: context,
        targetPage: PermissionRequestWidget(
            permission: Platform.isIOS ? Permission.camera : Permission.storage,
            permissionList: _list),
        callBack: (dynamic value) {
          // LogUtils.e('权限申请结果    ${value.toString()}');
          initDataNext();
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          child: Text(''),
        ),
      ),
    );
  }

  //初始化工具类
  void initDataNext() async {
    if (Platform.isIOS) {
      // Directory libDire = await getLibraryDirectory();
      // LogUtils.e("libDire ${libDire.path}");
    }
    //初始化
    await SPUtil.init();
    //读取一下标识
    bool isAgrement = await SPUtil.getBool("isAgrement");

    // LogUtils.e("isAgrement $isAgrement");

    // UserHepler.getInstance.init();

    if (isAgrement == null || !isAgrement) {
      isAgrement = await showProtocolFunction(context);
    }

    if (isAgrement) {
      //同意
      // LogUtils.e("同意协议");
      //保存一下标识
      SPUtil.save("isAgrement", true);

      next();
    } else {
      // LogUtils.e("不同意");
      closeApp();
    }
  }

  void closeApp() {
    SystemChannels.platform.invokeMethod("SystemNavigator.pop");
  }

  void next() async {
    //判断是否第一次安装应用
    bool isFirstInstall = await SPUtil.getBool("flutter_ho_isFirst");

    if (isFirstInstall == null) {
      //如果为null 则是第一次安装应用
      //引导 页面
      NavigatorUtil.pushPage(
          context: context, targetPage: FirstGuildPage(), isReplace: true);
    } else {
      //倒计时页面
      NavigatorUtil.pushPage(
          context: context, targetPage: WelcomePage(), isReplace: true);
    }
  }
}
